#include "groupsessiondetailwidget.h"
#include "sessiondetailwidget.h"
#include "debug.h"
#include "model/datacenter.h"
#include <QVBoxLayout>
#include <QScrollArea>
#include <QScrollBar>
#include <QPushButton>

using namespace model;

GroupSessionDetailWidget::GroupSessionDetailWidget(QWidget* parent)
    : QDialog(parent)
{
    // 1. 设置窗口的基本属性.
    this->setFixedSize(360, 500);
    this->setWindowTitle("群组详情");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    this->setStyleSheet("QDialog { background-color: rgb(255, 255, 255); }");
    this->setAttribute(Qt::WA_DeleteOnClose);

    // 2. 创建布局管理器
    QVBoxLayout* vlayout = new QVBoxLayout();
    vlayout->setSpacing(10);
    vlayout->setContentsMargins(30, 20, 10, 30);
    vlayout->setAlignment(Qt::AlignTop);
    this->setLayout(vlayout);

    // 3. 创建滚动区域
    // 3.1 创建 QScrollArea 对象
    QScrollArea* scrollArea = model::createScrollArea();

    // 3.2 创建一个 QScrollArea 内部的 QWidget
    QWidget* container = new QWidget();
    container->setStyleSheet(R"(QWidget{background-color: rgb(255, 255, 255);})");
    scrollArea->setWidget(container);

    // 3.3 给 container 里面添加一个 网格布局
    glayout = new QGridLayout();
    glayout->setSpacing(10);
    glayout->setContentsMargins(0, 0, 0, 0);
    glayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
    container->setLayout(glayout);

    // 3.4 把滚动区域, 添加到布局管理器中
    vlayout->addWidget(scrollArea);

    // 4. 添加 "添加按钮"
    AvatarItem* addBtn = new AvatarItem(QIcon(":/resource/image/cross.png"), "添加", false);
    glayout->addWidget(addBtn, 0, 0);

    // 5. 添加 "群聊名称"
    QLabel* groupNameTag = new QLabel();
    groupNameTag->setText("群聊名称");
    groupNameTag->setStyleSheet("QLabel {font-size: 14px; color: #333}");
    groupNameTag->setFixedHeight(40);
    groupNameTag->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    groupNameTag->setAlignment(Qt::AlignBottom | Qt::AlignLeft);    // 这里设置的 QLabel 在布局管理器中的对齐方式.
    vlayout->addWidget(groupNameTag);

    // 6. 添加 真实的群聊名字 和 修改按钮
    // 6.1 创建水平布局
    QHBoxLayout* hlayout = new QHBoxLayout();
    hlayout->setSpacing(0);
    hlayout->setContentsMargins(0, 0, 0, 0);
    vlayout->addLayout(hlayout);

    // 6.2 创建真实群聊名字的 label
    groupNameLabel = new QLabel();
    groupNameLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    groupNameLabel->setFixedHeight(40);
    groupNameLabel->setStyleSheet("QLabel { font-size: 14px; color: #666}");
    hlayout->addWidget(groupNameLabel, 1, Qt::AlignLeft);

    // 6.3 创建 "修改按钮"
    QPushButton* modifyBtn = new QPushButton("修改");
    modifyBtn->setFixedSize(60, 24);
    modifyBtn->setStyleSheet(R"(
        QPushButton{
            font-size: 12px;
            font-weight: 500;
            color: #555;
            border: 1px solid #ccc;
            background-color: #e8e8e8;
            border-radius: 6px;
            padding: 3px 8px;
        }
        QPushButton:hover{
            background-color: #e0e0e0;
            border-color: #bbb;
        }
        QPushButton:pressed{
            color: #ffffff;
            background-color: #07c160;
            border-color: #07c160;
        }
    )");
    modifyBtn->setIcon(QIcon(":/resource/image/modify.png"));
    modifyBtn->setIconSize(QSize(16, 16));

    hlayout->addWidget(modifyBtn, 0, Qt::AlignRight);

    // 7. 退出群聊按钮
    QPushButton* exitGroupBtn = new QPushButton();
    exitGroupBtn->setText("退出群聊");
    exitGroupBtn->setFixedHeight(42);
    exitGroupBtn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    exitGroupBtn->setStyleSheet(R"(
        QPushButton {
           background-color: #F44336;
           border: none;
           border-radius: 8px;
           color: white;
           font-size: 14px;
           padding: 8px 15px;
        }
        QPushButton:hover {
           background-color: #D32F2F;
        }
        QPushButton:pressed {
           background-color: #B71C1C;
        };
    )");
    vlayout->addWidget(exitGroupBtn);

// 此处构造假的数据用来测试界面
#if TEST_UI
    groupNameLabel->setText("人类吃喝行为研究小组");
    QIcon avatar(":/resource/image/yan.png");
    for (int i = 0; i < 30; ++i) {
        AvatarItem* item = new AvatarItem(avatar, "张三" + QString::number(i ), true);
        this->addMember(item);
    }
#endif

    // 8. 从服务器加载数据
    initData();
}

void GroupSessionDetailWidget::initData(){
    DataCenter* dataCenter = DataCenter::getInstance();
    connect(dataCenter, &DataCenter::getMemberListDone, this, &GroupSessionDetailWidget::initMembers);
    dataCenter->getMemberListAsync(dataCenter->getCurrentChatSessionId());
}

void GroupSessionDetailWidget::initMembers(const QString &chatSessionId){
    DataCenter* dataCenter = DataCenter::getInstance();
    QList<UserInfo>* memberList = dataCenter->getMemberList(chatSessionId);
    if (memberList == nullptr) {
        LOG() << "获取的成员列表为空! chatSessionId=" << chatSessionId;
        return;
    }
    // 遍历成员列表
    for (const auto& u : *memberList) {
        AvatarItem* avatarItem = new AvatarItem(u.avatar, u.nickname, true);
        this->addMember(avatarItem);
    }

    // 群聊名称, 此处先设成固定名称.
    groupNameLabel->setText("新的群聊");
}


void GroupSessionDetailWidget::addMember(AvatarItem *avatarItem){
    // 拿到滚动区域中的布局管理器, 取到成员变量即可.
    const int MAX_COL = 4;
    if (curCol >= MAX_COL) {
        // 换行操作
        ++curRow;
        curCol = 0;
    }
    glayout->addWidget(avatarItem, curRow, curCol);
    ++curCol;
}










